using AutojsController.ViewModels;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.VisualTree;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;

namespace AutojsController.Views;

public partial class GroupView : UserControl
{
    public GroupView()
    {
        InitializeComponent();
    }

    private void GroupedTabControl_PointerPressed(object sender, PointerPressedEventArgs e)
    {
        base.OnPointerPressed(e);
        var tabControl = (TabControl)sender;
        var pointer = e.GetCurrentPoint(this);
        
        if (pointer.Properties.IsRightButtonPressed)
        {
            var hitTest = this.InputHitTest(pointer.Position);
            var current = hitTest as Visual;

            while (current != null)
            {
                if (current is MobileItemView mobileItemView &&
                    mobileItemView.DataContext is MobileItemViewModel mobileItemViewModel)
                {
                    mobileItemViewModel.IsSelected = true;
                    break;
                }
                current = current.GetVisualParent();
            }

            var index = tabControl.SelectedIndex;
            if (DataContext is GroupViewModel grpVM && grpVM.MobileGroups[index].Mobiles.Where(m => m.IsSelected).Count() > 0)
                RemoveFromGroupMenuItem.IsVisible = true;
            else
                RemoveFromGroupMenuItem.IsVisible = false;
        }
    }

    private async void UngroupedMobiles_PointerPressed(object sender, PointerPressedEventArgs e)
    {
        if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed && DataContext is GroupViewModel grpvm)
        {
            var source = e.Source as Control;
            if (source != null && source.FindAncestorOfType<MobileItemView>() is MobileItemView mobView && mobView.DataContext is MobileItemViewModel mobVM)
                mobVM.IsSelected = true;
            var selectedMobiles = grpvm.UngroupedMobiles.Where(m => m.IsSelected).ToList();
            if (selectedMobiles.Any())
            {
                var dragData = new DataObject();
                dragData.Set("SelectedMobiles", selectedMobiles);
                await DragDrop.DoDragDrop(e, dragData, DragDropEffects.Move);
            }
        }
    }

    private void GroupedTabControl_DragOver(object sender, DragEventArgs e)
    {
        if (e.Data.Get("SelectedMobiles") is not null)
        {
            e.DragEffects = DragDropEffects.Move;
        }
    }

    private async void GroupedTabControl_Drop(object sender, DragEventArgs e)
    {
        if (DataContext is GroupViewModel grpvm && sender is TabControl tabControl && tabControl.SelectedItem is ViewModels.GroupedMobiles selectedGroup)
        {
            if (e.Data.Get("SelectedMobiles") is List<MobileItemViewModel> selectedMobiles)
            {
                await grpvm.AddMobilesToGroupAsync(selectedMobiles, selectedGroup.GroupId);
            }
        }
    }
}